

#include "SF_JustNote.h"
#include "../SQLite/SQLiteHandler.h"


typedef	struct	tagSF_Query_t
{
	CSQL_query	*query;
	int			id;
}SF_Query_t;

enum
{
	en_add_note	=	0,
	en_begin,
	en_commit,
	en_get_note_sum_list,
	en_get_note_detail_by_uuid,
	en_add_tags,
	en_get_tag_list,
	en_max,
};
const	char*  sql_insert_note	=	"insert into note values( ?, ?, ?, ?, ?, ?, ?, ?,? )";
const	char*  sql_get_note_detail_by_uuid	=	"select json from note where uuid=?";
const	char*  sql_get_tag_list	=	"select * from tags ";
const	char*  sql_begin		=	"begin";
const	char*  sql_commit		=	"commit";

static	SF_Query_t	g_stSF_Query[ en_max ]	=	{ 0 };

static	CSQLiteManager	*g_pCSQLiteManager	=	NULL;


//Select * From Account Limit 9 Offset 10;
int getNoteSummaryList( int _iBegin, int _iCount, vector<Summarylist_t> &_vecSummarylist )
{
	int		iRlt		=	XS_SUCCESS;
	char	acTemp[256]	=	"";
	if ( _iBegin > 0 )
	{
		--_iBegin;
		sprintf(acTemp, "Select * from note order by createtime desc limit %d offset %d", _iCount, _iBegin );
	}
	else if ( 0 == _iBegin )
	{
		sprintf(acTemp, "Select * from note order by createtime desc limit %d", _iCount );
	}
	else
	{
		return	-1;
	}

	
	SF_Query_t	stSF_Query	=	{ 0 };
	iRlt	=	g_pCSQLiteManager->Query_Create( &( stSF_Query.id ), &( stSF_Query.query ) );
	iRlt	=	stSF_Query.query->Prepare( acTemp );

	iRlt	=	stSF_Query.query->Step();
	while ( iRlt == XS_STEP_ROW )
	{
		Summarylist_t	stSummarylist	=	{ 0 };
		stSF_Query.query->GetField( 0, &( stSummarylist._id ) );

		char* pTemp = NULL; 
		stSF_Query.query->GetField( 2, &pTemp );
		stSummarylist.strSummary	=	pTemp;

		stSF_Query.query->GetField( 3, &( stSummarylist.dCreatetime ) );
		stSF_Query.query->GetField( 4, &( stSummarylist.dModifytime ) );
		stSF_Query.query->GetField( 5, &( stSummarylist.dNotifytime ) );
		stSF_Query.query->GetField( 6, &( stSummarylist.iStatus ) );
		stSF_Query.query->GetField( 7, &( stSummarylist.iFlg ) );

		pTemp = NULL; 
		stSF_Query.query->GetField( 8, &pTemp );
		stSummarylist.strUUID	=	pTemp;

		_vecSummarylist.push_back( stSummarylist );

		iRlt	=	stSF_Query.query->Step();
	}


	g_pCSQLiteManager->Query_Delete( stSF_Query.id );

	return	iRlt;
}

int getNoteSummaryListByTags( const string &_strTags, int _iBegin, int _iCount, vector<Summarylist_t> &_vecSummarylist )
{
	return	failure;
}

int searchNoteSummaryList( const vector<string> &_vecKeys, int _iBegin, int _iCount, vector<Summarylist_t> &_vecSummarylist )
{
	return	failure;
}

int getNoteSummaryListByDate( double _dDate, vector<Summarylist_t> &_vecSummarylist )
{
	return	failure;
}

int getNoteSummaryListByStatus( int _iStatus, vector<Summarylist_t> &_vecSummarylist )
{
	return	failure;
}

int deleteNote( int _iUUID )
{
	return	failure;
}

int addNote( const string &_strJson, Summarylist_t &_stSummarylist )
{
	int	iRlt	=	XS_SUCCESS;
	
	iRlt	=	g_stSF_Query[en_add_note].query->Bind( 2, (char*)_strJson.c_str(), _strJson.length() );
	iRlt	=	g_stSF_Query[en_add_note].query->Bind( 3, (char*)_stSummarylist.strSummary.c_str(), _stSummarylist.strSummary.length() );
	iRlt	=	g_stSF_Query[en_add_note].query->Bind( 4, _stSummarylist.dCreatetime );
	iRlt	=	g_stSF_Query[en_add_note].query->Bind( 5, _stSummarylist.dModifytime );
	iRlt	=	g_stSF_Query[en_add_note].query->Bind( 6, _stSummarylist.dNotifytime );
	iRlt	=	g_stSF_Query[en_add_note].query->Bind( 7, _stSummarylist.iStatus );
	iRlt	=	g_stSF_Query[en_add_note].query->Bind( 8, _stSummarylist.iFlg );
	iRlt	=	g_stSF_Query[en_add_note].query->Bind( 9, (char*)_stSummarylist.strUUID.c_str(), _stSummarylist.strUUID.length() );
	iRlt	=	g_stSF_Query[en_add_note].query->Step();
	iRlt	=	g_stSF_Query[en_add_note].query->Reset();

	int _iUUID	=	g_pCSQLiteManager->GetLastRowid();
	_stSummarylist._id=_iUUID;
	return	iRlt;
}

int modifyNote( int _iUUID, const string &_strJson, const Summarylist_t &_stSummarylist )
{
	return	failure;
}

int getNoteDetailByUUID( const string &strUuid, string &_detailjson )
{
	int		iRlt		=	XS_SUCCESS;
	//char	acTemp[256]	=	"";
	//sprintf(acTemp, "select json from note where uuid=\"%s\"", strUuid.c_str() );
	iRlt	=	g_stSF_Query[en_get_note_detail_by_uuid].query->Bind( 1, (char*)strUuid.c_str(), strUuid.length() );

	//SF_Query_t	stSF_Query	=	{ 0 };
	//iRlt	=	g_pCSQLiteManager->Query_Create( &( stSF_Query.id ), &( stSF_Query.query ) );
	//iRlt	=	stSF_Query.query->Prepare( acTemp );
	iRlt	=	g_stSF_Query[en_get_note_detail_by_uuid].query->Step();
	
	//iRlt	=	stSF_Query.query->Step();
	if ( iRlt == XS_STEP_ROW )
	{
		char* pTemp = NULL; 
		g_stSF_Query[en_get_note_detail_by_uuid].query->GetField( 0, &pTemp );
		_detailjson	=	pTemp;

	}

	iRlt	=	g_stSF_Query[en_get_note_detail_by_uuid].query->Reset();
	
	return	iRlt;
}
int addTags( vector<tagList_t> &_vectags )//external should confirm name is unique
{
	int		iRlt		=	XS_SUCCESS;
	string sql="insert into tags (uuid,name) values ";
	int size=_vectags.size();
	bool isfirst=true;
	for(int i=0;i<size;i++){
		tagList_t tag=_vectags.at(i);
		string temp;
		if(isfirst){
			temp="(\"";
			isfirst=false;
		}else{
			temp=",(\"";
		}
		temp.append(tag.strUUID);
		temp.append("\",\"");
		temp.append(tag.strName);
		temp.append("\")");
		sql.append(temp);
	}
	SF_Query_t	stSF_Query	=	{ 0 };
	iRlt	=	g_pCSQLiteManager->Query_Create( &( stSF_Query.id ), &( stSF_Query.query ) );
	iRlt	=	stSF_Query.query->Prepare( sql.c_str() );

	iRlt	=	stSF_Query.query->Step();
	if ( iRlt == XS_STEP_DONE )
	{
		iRlt		=	XS_SUCCESS;

	}


	g_pCSQLiteManager->Query_Delete( stSF_Query.id );
	return	iRlt;
}
int getTagList( vector<tagList_t> &_vectagList )
{
	int		iRlt		=	XS_SUCCESS;

	iRlt	=	g_stSF_Query[en_get_tag_list].query->Step();
	while ( iRlt == XS_STEP_ROW )
	{
		tagList_t	stTagRec;
		g_stSF_Query[en_get_tag_list].query->GetField( 0, &( stTagRec._id ) );

		char* pTemp = NULL; 
		g_stSF_Query[en_get_tag_list].query->GetField( 1, &pTemp );
		stTagRec.strUUID	=	pTemp;

		pTemp = NULL; 
		g_stSF_Query[en_get_tag_list].query->GetField( 2, &pTemp );
		stTagRec.strName	=	pTemp;

		g_stSF_Query[en_get_tag_list].query->GetField( 3, &( stTagRec.iIsEncode ) );
		g_stSF_Query[en_get_tag_list].query->GetField( 4, &( stTagRec.iTapCount ) );
		g_stSF_Query[en_get_tag_list].query->GetField( 5, &( stTagRec.iNoteCount ) );


		_vectagList.push_back( stTagRec );

		iRlt	=	g_stSF_Query[en_get_tag_list].query->Step();
	}
	g_stSF_Query[en_get_tag_list].query->Reset();
	if (iRlt==XS_STEP_DONE)
	{
		iRlt=XS_SUCCESS;
	}
	return	iRlt;
}

int editTagName( int _iUUID, const string &_strRename )
{
	return	failure;
}

int encodeTags( vector<int> &_vecUUIDs )
{
	return	failure;
}

int decodeTags( const vector<int> &_vecUUIDs )
{
	return	failure;
}

int setPassword( const string &_strOld, const string &_strNew, const string &_strEmail )
{
	return	failure;
}

int getPassword( string &_strPassword )
{
	return	failure;
}

int Initailize( const char *_path )
{
	int	iRlt	=	XS_FAILURE;

	if ( NULL != g_pCSQLiteManager )
	{
		delete	g_pCSQLiteManager;
		g_pCSQLiteManager	=	NULL;
	}

	g_pCSQLiteManager	=	new CSQLiteManager();
	if ( NULL != g_pCSQLiteManager )
	{
		iRlt	=	g_pCSQLiteManager->Initialize( _path );
	}

	for ( int i =0; i < en_max; ++i )
	{
		g_pCSQLiteManager->Query_Create( &(g_stSF_Query[i].id), &(g_stSF_Query[i].query) );
	}
	
	g_stSF_Query[en_add_note].query->Prepare( sql_insert_note );
	g_stSF_Query[en_get_note_detail_by_uuid].query->Prepare( sql_get_note_detail_by_uuid );
	g_stSF_Query[en_get_tag_list].query->Prepare( sql_get_tag_list );
	g_stSF_Query[en_begin].query->Prepare( sql_begin );
	g_stSF_Query[en_commit].query->Prepare( sql_commit );

	return	iRlt;
}

void UnInitialize()
{
	if ( NULL != g_pCSQLiteManager )
	{
		g_pCSQLiteManager->UnInitialize();

		delete	g_pCSQLiteManager;
		g_pCSQLiteManager	=	NULL;
	}

	for ( int i =0; i < en_max; ++i )
	{
		g_pCSQLiteManager->Query_Delete( g_stSF_Query[i].id );
	}
}
